﻿

namespace LS.Nc6.Application.System
{
    public class SysNoticeService : BaseService<SysNoticeEntity>, ISysNoticeService, ITransient
    {
        /// <summary>
        /// 获得未读消息的数量
        /// </summary>
        /// <returns></returns>
        public DataTable GetUnNotifyNum(string where)
        {
            return null;
        }
        /// <summary>
        /// 更加ids 设置未读消息为已读
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<int> SetNoticeToReads(string ids,long userId)
        {
            var arryids = ids.GetStringToLongArryNoNull();
            return await DbContext.Instance.Updateable<SysNoticeUserEntity>().SetColumns(a => a.ReadStatus == 1).SetColumns(a => a.ReadTime == DateTime.Now).Where(a => arryids.Contains(a.NoticeId) && a.UserId == userId).ExecuteCommandAsync();
        }
        //插入消息
        public async Task<int> InsertNotice(SysNoticeEntity notice, List<SysNoticeUserEntity> noticeUserList)
        {
            try
            {
                DbContext.Instance.BeginTran();
                long noticeId= await DbContext.Instance.Insertable(notice).ExecuteReturnBigIdentityAsync();
                if (noticeUserList != null && noticeUserList.Count > 0 && notice.Status == 1)
                {
                     noticeUserList.ForEach(a => a.NoticeId = noticeId);
                     await DbContext.Instance.Insertable(noticeUserList).ExecuteCommandAsync();
                }
                DbContext.Instance.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                DbContext.Instance.RollbackTran();
                throw;
            }
        }


        public async Task<int> UpdateNotice(SysNoticeEntity notice, List<SysNoticeUserEntity> noticeUserList)
        {
            try
            {
                DbContext.Instance.BeginTran();
                await DbContext.Instance.Updateable(notice).ExecuteCommandAsync();
                if (noticeUserList != null && noticeUserList.Count > 0 && notice.Status==1)
                {
                    noticeUserList.ForEach(a => a.NoticeId = notice.Id);
                    await DbContext.Instance.Insertable(noticeUserList).ExecuteCommandAsync();
                }
                DbContext.Instance.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                DbContext.Instance.RollbackTran();
                throw;
            }
        }


        public async Task<SqlSugarPagedList<SysNoticeEntity>> PageNoticeList(Expression<Func<SysNoticeEntity, SysNoticeUserEntity, bool>> expression, int page, int limit, Expression<Func<SysNoticeEntity, SysNoticeUserEntity, object>> expressionOrder)
        {
            return await DbContext.Instance.Queryable<SysNoticeEntity>().LeftJoin<SysNoticeUserEntity>((a, b) => a.Id == b.NoticeId).Where(expression).OrderBy(expressionOrder).ToPagedListAsync(page, limit);
        }

        public async Task<List<dynamic>> NoticeList(Expression<Func<SysNoticeEntity, SysNoticeUserEntity, bool>> expression, int limit, 
            Expression<Func<SysNoticeEntity, SysNoticeUserEntity, object>> expressionOrder, Expression<Func<SysNoticeEntity, SysNoticeUserEntity, dynamic>> expressionSelect)
        {
          
           return   await   DbContext.Instance.Queryable<SysNoticeEntity>().LeftJoin<SysNoticeUserEntity>((a, b) => a.Id == b.NoticeId).Where(expression).OrderByDescending(expressionOrder).Select(expressionSelect).Take(limit).ToListAsync();
       

        }
    }
}
